package com.woniu.gym.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.core.exception.account.AccountException;
import com.woniu.core.exception.account.AccountExceptionCode;
import com.woniu.core.exception.gym.GymException;
import com.woniu.core.exception.gym.GymExceptionCode;
import com.woniu.core.util.PageInfo;
import com.woniu.gym.dao.TGymBaseMapper;
import com.woniu.gym.dao.TRecruitConditionMapper;
import com.woniu.gym.dto.RecruitConditionDto;
import com.woniu.gym.form.*;
import com.woniu.gym.model.TGymBase;
import com.woniu.gym.model.TRecruitCondition;
import com.woniu.gym.service.TRecruitConditionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年04月02日
 */
@Service
public class TRecruitConditionServiceImpl extends ServiceImpl<TRecruitConditionMapper, TRecruitCondition> implements TRecruitConditionService {
    @Resource
    private TGymBaseMapper tGymBaseMapper;
    @Resource
    private TRecruitConditionMapper tRecruitConditionMapper;

    @Override
    public RecruitConditionDto addRecruit(AddRecruitForm form) {
        QueryWrapper<TGymBase> wrapper = new QueryWrapper<>();
        wrapper.eq("gym_num",form.getGymNum());
        TGymBase tGymBase = tGymBaseMapper.selectOne(wrapper);
        if(tGymBase==null) throw new GymException(GymExceptionCode.GYM_NOT_EXIST);
        TRecruitCondition recruitCondition = BeanUtil.toBean(form, TRecruitCondition.class);
        recruitCondition.setJobDetails(JSONUtil.toJsonStr(form.getJobDetailsForm()));
        recruitCondition.setRecruitState(1);
        tRecruitConditionMapper.insert(recruitCondition);
        //返回给前端
        RecruitConditionDto dto = BeanUtil.toBean(form, RecruitConditionDto.class);
        dto.setRecruitId(recruitCondition.getRecruitId());
        dto.setRecruitState(recruitCondition.getRecruitState());

        return dto;
    }

    @Override
    public TRecruitCondition delJobPostings(Integer recruitId,String gymNum) {
        TRecruitCondition recruitCondition = tRecruitConditionMapper.selectById(recruitId);
        if(recruitCondition==null){
            throw new AccountException(AccountExceptionCode.CANT_FIND_THIS_REVRUITMENT_INFORMATION);
        }

        if(!recruitCondition.getGymNum().equals(gymNum)){
            throw new AccountException(AccountExceptionCode.YOU_DONT_HAVE_PERMISSION_TO_OPERATE);
        }
        recruitCondition.setRecruitState(0);
        Integer b = tRecruitConditionMapper.updateById(recruitCondition);
        if(b<=0)throw new AccountException(AccountExceptionCode.DELETE_FAILED);
        return recruitCondition;
    }

    @Override
    public PageInfo<RecruitConditionDto> findJobPostingsById(FindJobByGymNumForm form) {
        QueryWrapper<TGymBase> wrapper = new QueryWrapper<>();
        wrapper.eq("gym_num",form.getGymNum());
        TGymBase tGymBase = tGymBaseMapper.selectOne(wrapper);
        if(tGymBase==null) throw new GymException(GymExceptionCode.GYM_NOT_EXIST);

        Page<TRecruitCondition> page = new Page<>(form.getPageNum()==null?1:form.getPageNum(),form.getPageSize()==null?5: form.getPageSize());
        QueryWrapper<TRecruitCondition> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("gym_num",form.getGymNum()).eq("recruit_state",1);
        if(ObjectUtil.isAllNotEmpty(form.getCondition())){
            queryWrapper.like("job_title",form.getCondition());
            queryWrapper.or();
            queryWrapper.like("job_details",form.getCondition());
        }
        tRecruitConditionMapper.selectPage(page,queryWrapper);
        List<TRecruitCondition> list = page.getRecords();
        ArrayList<RecruitConditionDto> data = new ArrayList<>();
        for (TRecruitCondition trc : list) {
            RecruitConditionDto dto = BeanUtil.toBean(trc, RecruitConditionDto.class);
            dto.setJobDetailsForm(JSONUtil.toBean(trc.getJobDetails(), JobDetailsForm.class));
            data.add(dto);
        }
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setList(data);

        return pageInfo;
    }

    @Override
    public PageInfo<RecruitConditionDto> findAllJob(FindAllJobForm form) {
        Page<TRecruitCondition> page = new Page<>(form.getPageNum()==null?1:form.getPageNum(),form.getPageSize()==null?5: form.getPageSize());
        QueryWrapper<TRecruitCondition> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("recruit_state",1);
        if(ObjectUtil.isAllNotEmpty(form.getCondition())){
            queryWrapper.like("job_title",form.getCondition());
            queryWrapper.or();
            queryWrapper.like("job_details",form.getCondition());
        }
        tRecruitConditionMapper.selectPage(page,queryWrapper);
        List<TRecruitCondition> list = page.getRecords();
        ArrayList<RecruitConditionDto> data = new ArrayList<>();
        for (TRecruitCondition trc : list) {
            RecruitConditionDto dto = BeanUtil.toBean(trc, RecruitConditionDto.class);
            dto.setJobDetailsForm(JSONUtil.toBean(trc.getJobDetails(), JobDetailsForm.class));
            data.add(dto);
        }
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        pageInfo.setList(data);

        return pageInfo;
    }

    @Override
    public Boolean modifyRecruit(ModifyRecruitForm form) {
        TRecruitCondition recruitCondition = tRecruitConditionMapper.selectById(form.getRecruitId());
        if(recruitCondition==null || recruitCondition.getRecruitState()==0){
            throw new AccountException(AccountExceptionCode.CANT_FIND_THIS_REVRUITMENT_INFORMATION);
        }

        if(!recruitCondition.getGymNum().equals(form.getGymNum())){
            throw new AccountException(AccountExceptionCode.YOU_DONT_HAVE_PERMISSION_TO_OPERATE);
        }
        recruitCondition.setJobTitle(form.getJobTitle());
        recruitCondition.setJobDetails(JSONUtil.toJsonStr(form.getJobDetailsForm()));
        recruitCondition.setJobSalary(form.getJobSalary());
        recruitCondition.setCreateName(form.getCreateName());
        recruitCondition.setCreatePhone(form.getCreatePhone());
        Integer b = tRecruitConditionMapper.updateById(recruitCondition);
        if(b<=0)throw new AccountException(AccountExceptionCode.DELETE_FAILED);

        return true;
    }
}
